package com.jnr.huatu.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.jnr.huatu.R;
import com.jnr.huatu.adapter.MyPagerAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片轮播
 * @author：景辉
 * created at 2017/2/15 0015 下午 4:12
 */
public class CarouselActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    // 记录当前的页数
    private int mCount = 0;
    // 开始
    public static final int START = -1;
    // 停止
    public static final int STOP = -2;
    // 更新
    public static final int UPDATE = -3;
    // 接受传过来的当前页面数
    public static final int RECORD = -4;
    private List<ImageView> mList;
    private MyPagerAdapter mAdapter;
    private List<String> urlList;
    private ImageView dot1, dot2, dot3, dot0;
    private ImageView[] dots = new ImageView[4];
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
                case START:
                    handler.sendEmptyMessageDelayed(UPDATE, 3000);
                    break;
                case STOP:
                    handler.removeMessages(UPDATE);
                    break;
                case UPDATE:
                    mCount++;
                    mViewPager.setCurrentItem(mCount);
                    break;
                case RECORD:
                    mCount = msg.arg1;
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_carousel);
        initView();
        init();
        setListener();
        mAdapter = new MyPagerAdapter(mList);
        mViewPager.setAdapter(mAdapter);
        int i = Integer.MAX_VALUE / 2 % mList.size();
        // 默认在中间，让用户看不到边界
        mViewPager.setCurrentItem(Integer.MAX_VALUE / 2 - i);
        handler.sendEmptyMessage(START);
    }

    private void init() {
        urlList = new ArrayList<>();
        mList = new ArrayList<>();
        urlList.add("http://10.100.21.11/123/zq_test201702151118qqo.jpeg");
        urlList.add("http://10.100.21.11/123/zq_test201702151132bco.jpeg");
        urlList.add("http://10.100.21.11/123/zq_test201702151132sks.jpeg");
        urlList.add("http://10.100.21.11/123/zq_test201702151132ccd.jpeg");
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)//设置图片是否缓存到内存中
                .displayer(new RoundedBitmapDisplayer(50))
                .displayer(new FadeInBitmapDisplayer(100))
                .cacheOnDisk(true)//设置图片是否缓存到SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565).build();

        ImageView imageView;
        for (int i = 0; i < urlList.size(); i++) {
            imageView = new ImageView(CarouselActivity.this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            //图片加载
            ImageLoader.getInstance().displayImage(urlList.get(i), imageView, options);
            mList.add(imageView);
            imageView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(CarouselActivity.this, "点击了"+mCount% mList.size(), Toast.LENGTH_SHORT).show();
                    System.out.println("==========m==" + mCount % mList.size());
                    // 这里写点击图片的操作 mCount % mList.size()这个点击的第几个图片
                }
            });
        }
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        dot0 = (ImageView) findViewById(R.id.dot1);
        dot1 = (ImageView) findViewById(R.id.dot2);
        dot2 = (ImageView) findViewById(R.id.dot3);
        dot3 = (ImageView) findViewById(R.id.dot4);
        dots[0] = dot0;
        dots[1] = dot1;
        dots[2] = dot2;
        dots[3] = dot3;
        dot0.setSelected(true);
    }

    private void setListener() {
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                handler.sendMessage(Message.obtain(handler, RECORD, arg0, 0));
                // 下面是控制点的变化
                int j = arg0 % mList.size();
                for (int i = 0; i < dots.length; i++) {
                    dots[i].setSelected(false);
                }
                dots[j].setSelected(true);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                switch (arg0) {
                    // 当滑动时让当前轮播停止
                    case ViewPager.SCROLL_STATE_DRAGGING:
                        handler.sendEmptyMessage(STOP);
                        break;
                    // 滑动停止时继续轮播
                    case ViewPager.SCROLL_STATE_IDLE:
                        handler.sendEmptyMessage(START);
                        break;
                }
            }
        });
    }
}
